package com.baturu.vin.benz.transformer;

import com.baturu.vin.benz.dto.BenzSaDescDTO;
import com.baturu.vin.benz.dto.BenzSpringsDTO;
import com.baturu.vin.benz.dto.BenzSuperCarConfigurationDTO;
import com.baturu.vin.dto.superEPC.SEPCCarTypeDetailDTO;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * BenzSuperDashBroadTransformerService
 * 车型配置类型转换Service
 *
 * @author majianming
 * @date 2019-05-06 13:53
 */
@Service
public class BenzDashBroadTransformerService {
    /**
     * 将弹簧属性转换为拓展属性
     *
     * @param benzSpringsDTO 车型弹簧属性
     * @return
     */
    List<SEPCCarTypeDetailDTO> springTransform(BenzSpringsDTO benzSpringsDTO) {
        if (Objects.isNull(benzSpringsDTO)) {
            return Collections.emptyList();
        }
        List<SEPCCarTypeDetailDTO> springDetails = Lists.newArrayList();

        BenzSpringsDTO.SpringDesc frontSpringStrut = benzSpringsDTO.getFrontSpringStrut();
        springDetails.add(SEPCCarTypeDetailDTO.builder().code(frontSpringStrut.getKey()).desc(frontSpringStrut.getDesc()).value(frontSpringStrut.getValue()).build());

        BenzSpringsDTO.SpringDesc frontSpring = benzSpringsDTO.getFrontSpring();
        springDetails.add(SEPCCarTypeDetailDTO.builder().code(frontSpring.getKey()).desc(frontSpring.getDesc()).value(frontSpring.getValue()).build());

        BenzSpringsDTO.SpringDesc rearSpring = benzSpringsDTO.getRearSpring();
        springDetails.add(SEPCCarTypeDetailDTO.builder().code(rearSpring.getKey()).desc(rearSpring.getDesc()).value(rearSpring.getValue()).build());

        BenzSpringsDTO.SpringDesc rearSpringSuppl = benzSpringsDTO.getRearSpringSuppl();
        springDetails.add(SEPCCarTypeDetailDTO.builder().code(rearSpringSuppl.getKey()).desc(rearSpringSuppl.getDesc()).value(rearSpringSuppl.getValue()).build());

        if (CollectionUtils.isEmpty(springDetails)) {
            return Collections.emptyList();
        }
        return Lists.newArrayList(SEPCCarTypeDetailDTO.builder().code("springs").desc("弹簧").subValue(springDetails).build());
    }

    /**
     * dashBroad 底盘属性
     *
     * @param benzSuperCarConfigurationDTO 车型配置
     * @return
     */
    List<SEPCCarTypeDetailDTO> chassisDetailsTransform(BenzSuperCarConfigurationDTO benzSuperCarConfigurationDTO) {

        Map<String, List<BenzSaDescDTO>> benSaDescByCode = benzSuperCarConfigurationDTO.getBenzSaDescDTOS().stream().collect(Collectors.groupingBy(BenzSaDescDTO::getSaCode));

//        底盘
        List<SEPCCarTypeDetailDTO> chassisDetails = Lists.newArrayList();
        chassisDetails.add(SEPCCarTypeDetailDTO.builder().code("salesModel").desc("销售型号").value(benzSuperCarConfigurationDTO.getSalesModel()).build());

        // 任务单号
        String orderNumber = benzSuperCarConfigurationDTO.getOrderNumber();
        if (StringUtils.isNotBlank(orderNumber)) {
            chassisDetails.add(SEPCCarTypeDetailDTO.builder().code("orderNumber").desc("任务单号").value(orderNumber).build());
        }

//        交货日期
        if (StringUtils.isNotBlank(benzSuperCarConfigurationDTO.getProductDate())) {
            chassisDetails.add(SEPCCarTypeDetailDTO.builder().code("deliveryDate").desc("交货日期").value(benzSuperCarConfigurationDTO.getProductDate()).build());
        }
//        油漆代码1
        String paint1 = benzSuperCarConfigurationDTO.getPaint1();
        if (StringUtils.isNotBlank(paint1)) {
            List<BenzSaDescDTO> benzSaDescDTOS = benSaDescByCode.get(paint1);
            if (CollectionUtils.isNotEmpty(benzSaDescDTOS)) {
                String paint1Desc = benzSaDescDTOS.stream().map(BenzSaDescDTO::getSaDesc).collect(Collectors.joining(","));
                chassisDetails.add(SEPCCarTypeDetailDTO.builder().code("paint1").desc("油漆代码1").value(paint1 + " " + paint1Desc).build());
            }
        }
//        装备
        String interior = benzSuperCarConfigurationDTO.getInterior();
        if (StringUtils.isNotBlank(interior)) {
            List<BenzSaDescDTO> benzSaDescDTOS = benSaDescByCode.get(interior);
            if (CollectionUtils.isNotEmpty(benzSaDescDTOS)) {
                String interior1Desc = benzSaDescDTOS.stream().map(BenzSaDescDTO::getSaDesc).collect(Collectors.joining(","));
                chassisDetails.add(SEPCCarTypeDetailDTO.builder().code("interior").desc("装备").value(interior + " " + interior1Desc).build());
            }
        }
//        发动机编号
        if (StringUtils.isNotBlank(benzSuperCarConfigurationDTO.getEngine())) {
            chassisDetails.add(SEPCCarTypeDetailDTO.builder().code("engineNo").desc("发动机编号").value(benzSuperCarConfigurationDTO.getEngine()).build());
        }
//        机组信息
        if (StringUtils.isNotBlank(benzSuperCarConfigurationDTO.getMotorSachNummer())) {
            chassisDetails.add(SEPCCarTypeDetailDTO.builder().code("motorSachNummer").desc("机组信息").value(benzSuperCarConfigurationDTO.getMotorSachNummer()).build());
        }
//        零件款式结构模型
        if (StringUtils.isNotBlank(benzSuperCarConfigurationDTO.getEtvBM())) {
            chassisDetails.add(SEPCCarTypeDetailDTO.builder().code("etvBM").desc("零件款式结构模型").value(benzSuperCarConfigurationDTO.getEtvBM()).build());
        }
        //        车型配置文本
        if (StringUtils.isNotBlank(benzSuperCarConfigurationDTO.getZuText())) {
            chassisDetails.add(SEPCCarTypeDetailDTO.builder().code("zuText").desc("文本").value(benzSuperCarConfigurationDTO.getZuText()).build());
        }

        // 变速箱
        if (StringUtils.isNotBlank(benzSuperCarConfigurationDTO.getTransmission())) {
            chassisDetails.add(SEPCCarTypeDetailDTO.builder().code("transmission").desc("变速箱").value(benzSuperCarConfigurationDTO.getTransmission()).build());
        }

        if (CollectionUtils.isEmpty(chassisDetails)) {
            return Collections.emptyList();
        }


//        chassisDetails.add(SEPCCarTypeDetailDTO.builder().code("salesName").desc("销售名称").value(superCarInfoDTO.getChassbm() + " " + benzSuperCarConfigurationDTO.getSalesModel()).build());
        return Lists.newArrayList(SEPCCarTypeDetailDTO.builder().code("chassis").desc("底盘").subValue(chassisDetails).build());
    }

    /**
     * dashBroad saCode
     *
     * @param benzSuperCarConfigurationDTO 车型配置
     * @return
     */
    List<SEPCCarTypeDetailDTO> saCodeTransform(BenzSuperCarConfigurationDTO benzSuperCarConfigurationDTO) {
        //        sa代码
        List<SEPCCarTypeDetailDTO> saDesc = benzSuperCarConfigurationDTO.getBenzSaDescDTOS().stream().map(l ->
                SEPCCarTypeDetailDTO.builder()
                        .code(l.getSaCode())
                        .desc(l.getSaCode())
                        .value(l.getSaDesc()).build()).collect(Collectors.toList());

        if (CollectionUtils.isEmpty(saDesc)) {
            return Collections.emptyList();
        }
        return Lists.newArrayList(SEPCCarTypeDetailDTO.builder().code("saCode").desc("SA代码").subValue(saDesc).build());
    }
}
